package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.vo.CourseInfoVO;
import com.atguigu.eduservice.entity.vo.CoursePublishVO;
import com.atguigu.eduservice.entity.vo.CourseQuery;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-05-15
 */
@Slf4j
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    EduCourseService eduCourseService;


    //课程列表接口
    @GetMapping()
    public R getCourseList(){
        List<EduCourse> list = eduCourseService.list(null);
        return R.ok().data("list",list);
    }

    //课程列表条件查询带分页
    @PostMapping("pageCourseCondition/{current}/{limit}")
    public R pageCourseCondition(@PathVariable("current") Integer current,
                                 @PathVariable("limit") Integer limit,
                                 @RequestBody(required = false) CourseQuery courseQuery){
        Page<EduCourse> pageCourse = new Page<>(current,limit);
        eduCourseService.pageQuery(pageCourse,courseQuery);
        List<EduCourse> records = pageCourse.getRecords();

        //总记录
        long total = pageCourse.getTotal();
        return R.ok().data("total",total).data("rows",records);
    }
    //添加课程基本信息
    @PostMapping("addCourseInfo")
    private R addCourseInfo(@RequestBody CourseInfoVO courseInfoVO) {
        String id = eduCourseService.saveCourseInfo(courseInfoVO);
        return R.ok().data("courseId",id);
    }


    //根据课程id查询基本信息
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable("courseId") String courseId){
        CourseInfoVO courseInfoVO = eduCourseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVO",courseInfoVO);
    }

    //修改课程信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVO courseInfoVO){
        eduCourseService.updateCourseInfo(courseInfoVO);
        return R.ok();
    }

    //根据课程id查询课程确认的信息
    @GetMapping("getPublishCourseInfo/{courseId}")
    public R getPublishCourseInfo(@PathVariable("courseId") String courseId){
        CoursePublishVO coursePublishVO = eduCourseService.publishCourseInfo(courseId);
        return R.ok().data("coursePublishVO",coursePublishVO);
    }

    //课程最终发布
    //修改课程状态
    @PostMapping("publishCourse/{courseId}")
    public R publishCourse(@PathVariable("courseId") String courseId){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(courseId);
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return R.ok();
    }
    //删除课程
    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable("courseId") String courseId){
        eduCourseService.removeCourse(courseId);
        return R.ok();

    }
}

